/**
 * Created by way on 2016/12/6.
 */
import cfg from '../config';
import {format} from './kutil';

export default class Ws {
  static Status = {
    close: 0,
    open: 1,
    opened: 2
  };

  constructor() {
    this.status = Ws.Status.close;
    this.ws = this.open();
    this.status = Ws.Status.open;
  }

  open(cb) {
    if (!WebSocket) {
      console.log('WebSocket not supported!');
      return;
    }

    // console.log(location.host);
    const ws = new WebSocket(`ws://${WS_URL}`);
    if (ws) {
      ws.onopen = w => {
        console.log('opened...');
        ws.send('Hello WebSocket!');
        this.status = Ws.Status.opened;

        if (this.onopen)
          this.onopen(w);
        if (cb)
          cb();
        // ws.binaryType = "arraybuffer";
        // var a new Unit8Array([8,6,7,5,3,0,9]);
        // ws.send(a.buffer );
      };

      ws.onmessage = w => {
        console.log(w);
        if (this.onmsg)
          this.onmsg(w.data.toString());
        //JSON.parse(w.data).M
      };

      ws.onclose = w => {
        console.log('closed.');
        this.status = Ws.Status.close;
        this.ws = null;
        if (this.onclose)
          this.onclose(w);
      };

      ws.onerror = w => {
        console.log('error!');
        if (this.onerror)
          this.onerror();
      };
    } else
      console.log('连接消息服务器失败！');

    return ws;
  }

  send(s) {
    if (this.ws) {
      this.ws.send(s);
    } else {
      this.ws = this.open(() => {
        if (this.ws) {
          this.ws.send(s);
        }
      });
    }
  }

  //心跳
  heart(f, s) {
    let msg = format('{"H":%s, "F":"%s"}', s, f);
    this.send(msg);
  }

  // 登记
  reg(f, s = '1,""') {
    s = format('{"R":[%s], "F":"%s"}', s, f);
    this.send(s);
  }

  // 订阅 状态 1/0
  sub(f, t, s) {
    const msg = format('{"S":%d, "F":"%s", "T":"%s"}',
      s, f, t);
    this.send(msg);
  }

  // 消息
  msg(f, t, fw, tp, sq, st, s) {
    let msg = s;
    msg = msg.replace(/([^\\])"/g, '$1\\"');  // /(?<!\\)"/g 不支持
    let tm = parseInt(new Date().getTime() - Date.parse('2000/01/01'));
    msg = format('{"M":"%s", "F":"%s", "T":"%s", "FW":"%s", "TM":%d, "TP":"%s", "SQ":%d, "ST":%d}',
      msg, f, t, fw, tm, tp, sq, st);
    this.send(msg);
  }

  // 信息
  info(f, t, s) {
    let msg = s;
    msg = msg.replace(/([^\\])"/g, '$1\\"');  // /(?<!\\)"/g 不支持
    msg = format('{"I":"%s", "F":"%s", "T":"%s"}',
      msg, f, t);
    this.send(msg);
  }

  // 抢答
  answer(f, t, s) {
    let msg = s;
    msg = msg.replace(/([^\\])"/g, '$1\\"');  // /(?<!\\)"/g 不支持
    msg = format('{"M.A":"%s", "F":"%s", "T":"%s"}',
      msg, f, t);
    this.send(msg);
  }

  // 群发
  group(f, t, fw, tp, sq, st, s) {
    let msg = s;
    msg = msg.replace(/([^\\])"/g, '$1\\"');  // /(?<!\\)"/g 不支持
    let tm = parseInt(new Date().getTime() - Date.parse('2000/01/01'));
    msg = format('{"M.G":"%s", "F":"%s", "T":"%s", "FW":"%s", "TM":%d, "TP":"%s", "SQ":%d, "ST":%d}',
      msg, f, t, fw, tm, tp, sq, st);
    this.send(msg);
  }

  // 标签广播
  tag(f, t, s) {
    let msg = s;
    msg = msg.replace(/([^\\])"/g, '$1\\"');  // /(?<!\\)"/g 不支持
    msg = format('{"M.T":"%s", "F":"%s", "T":"%s"}',
      msg, f, t);
    this.send(msg);
  }

  // 关闭
  close() {
    this.ws.close();
  }
}
